/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Console.java
 *
 * Created on Apr 10, 2009, 5:40:53 PM
 */

package terminalapp;

import java.awt.event.KeyEvent;

/**
 *
 * @author Kostya
 */
public class Console extends javax.swing.JFrame implements TerminalModelChangedListener{

    /** Creates new form Console */

    TerminalModel model = null;

    public Console(TerminalModel aModel) {
        this();
        model = aModel;
        model.addTerminalModelChangedListener(this);
        jTextArea1.setAutoscrolls(true);
    }

    public Console() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setFont(new java.awt.Font("Consolas", 0, 12));

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Monaco", 0, 12)); // NOI18N
        jTextArea1.setRows(5);
        jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextArea1KeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextArea1KeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(jTextArea1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyPressed
        //Write here
        int keyCode = evt.getKeyChar();
        if(keyCode==10)
            keyCode = 13;
        if(!evt.isActionKey())
            model.sendCharacter(keyCode);
//        Log.debug(this, "Send character: alt", evt.isAltDown(), "Action:", evt.isActionKey(), keyCode, (char)keyCode);
        if(evt.isAltDown()){
            model.sendCharacters(new int[]{27, keyCode});
        }
        if(evt.isActionKey()){
            switch (evt.getKeyCode()){
                case KeyEvent.VK_UP:
                    model.sendCharacters(new int[]{27, 0x5b, 0x41});
                    break;
                case KeyEvent.VK_DOWN:
                    model.sendCharacters(new int[]{27, 0x5b, 0x42});
                    break;
                case KeyEvent.VK_RIGHT:
                    model.sendCharacters(new int[]{27, 0x5b, 0x43});
                    break;
                case KeyEvent.VK_LEFT:
                    model.sendCharacters(new int[]{27, 0x5b, 0x44});
                    break;
            }
        }
        evt.setKeyChar((char)0);
        evt.setKeyCode(0);

    }//GEN-LAST:event_jTextArea1KeyPressed

    private void jTextArea1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyTyped
//        Log.debug(this, "Pressed: "+evt.getKeyCode(), evt.getKeyChar(), (int)evt.getKeyChar());
//        int keyCode = evt.getKeyChar();
//        if(keyCode==10)
//            keyCode = 13;
//        Log.debug(this, "Send character: alt", evt.isAltDown(), keyCode, (char)keyCode);
//        if(evt.isAltDown()){
//            model.sendCharacters(new int[]{27, keyCode});
//        }
//        model.sendCharacter(keyCode);
    }//GEN-LAST:event_jTextArea1KeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    public void modelChanged() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < model.getLineCount(); i++) {
            buffer.append(String.format("%3d", i)+": ");
            String line = model.getLine(i);
//            while(line.length()>model.lineWidth){
//                buffer.append(line.substring(0, model.lineWidth-1));
//                line = line.substring(model.lineWidth);
//                buffer.append("\n");
//            }
            buffer.append(line);
            buffer.append("\n");
        }
        jTextArea1.setText(buffer.toString());
        jScrollPane1.getVerticalScrollBar().setValue(100000);
    }

    public void titleChanged(String aTitle){
        setTitle(aTitle);
    }

}
